/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 30, 2013  2:45:04 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.interfaces.controller.sd.outbound.sales
 * File Name : CustomerBacklogsController.java
 * TODO
 */
package com.tis.interfaces.controller.sd.outbound.sales;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import promis.utility.CommonService;
import com.tis.database.entitys.promis.Customer;
import com.tis.database.entitys.staging.ApplicationLoging;
import com.tis.database.services.promis.ICustomerServices;
import com.tis.interfaces.controller.commons.CommonController;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 30, 2013 : 2:45:04 PM 
 * CreateBy=Chatree Siangklom
 * TODO
 */
@Controller
@RequestMapping(value=CustomerBacklogsController.url)
public class CustomerBacklogsController extends CommonController {
	
	private static final Logger logging = LoggerFactory.getLogger(CustomerBacklogsController.class);
	public static final String  url = "/sd/outbound/sales/customer/backlogs";
	public static final String  uri = "/sd/outbound/sales/backlogs";
	
	@RequestMapping(value="index")
	public String Index(HttpServletRequest request, Model model) throws Exception {
		logging.info("/sd/outbound/sales/customer/backlogs/index {Index}");
		try {
			setCurrentAppsInstanceNo();
			KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "W", "WELCOME", null, getCurrentAppsInstanceNo()));
			
			//Put History
			try { UserAccessTime(request, this.toString()); }catch(Exception e) {e.printStackTrace();}
			
			if(!UserLoginValidation((request)))
				return "redirect:/login";
			
			CustomerBacklogsForm form = new CustomerBacklogsForm();
			form.setMaxResult("5000");
			
			logging.info("form =" + form.toString());
			model.addAttribute("ObjectForm", form);
			
		} catch (Exception e) {
			logging.error(e.getMessage());
			e.printStackTrace();
			KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "E", e.getMessage(), null, getCurrentAppsInstanceNo()));
			throw e;
		}
		
		return uri + "/customer_bl";
	}
	
	@RequestMapping(value="search", method = RequestMethod.POST)
	public String Search(HttpServletRequest request, Model model, @ModelAttribute("ObjectForm") @Valid	CustomerBacklogsForm form, BindingResult result) throws Exception {
		try {
			logging.info("/sd/outbound/sales/customer/backlogs/search {search}");
			setCurrentAppsInstanceNo();
			KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "W", "WELCOME", null, getCurrentAppsInstanceNo()));
			
			//Put History 		
			try { UserAccessTime(request, this.toString()); }catch(Exception e) {e.printStackTrace();}
			
			if(!UserLoginValidation((request)))
				throw new Exception("Can't Search, Please Login first !!!!");
			
		} catch (Exception e) {
			e.printStackTrace();
			logging.error(e.getMessage());
			KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "E", e.getMessage(), null, getCurrentAppsInstanceNo()));
			
			throw e;
		}
		
		return uri + "/customer_bl";
	}
	
	@Autowired
	ICustomerServices iCustomerServices;
	
	@RequestMapping(value="search/json", method = RequestMethod.GET, headers="accept=application/json")
	@ResponseBody
	public List<Customer> SearchBacklogs(
														@RequestParam(value="customerCode", required=false) String customerCode,
														@RequestParam(value="customerName", required=false) String customerName,
														@RequestParam(value="idNo", required=false) String idNo,
														@RequestParam(value="taxId", required=false) String taxId,														
														@RequestParam(value="maxResult", required=false) String maxResult,
														HttpServletRequest request, 
														HttpServletResponse response) throws Exception {
		
		logging.info(url + "search/json {Search} ::: method = RequestMethod.GET");
		setCurrentAppsInstanceNo();
		KeepLogs(new ApplicationLoging(getCurrentAppsInstanceNo(), "W", "WELCOME", null, getCurrentAppsInstanceNo()));
		
		if(!UserLoginValidation((request))) {
			response.setStatus(500);
			return null;
		}
		
		try {
			
			if(!UserLoginValidation((request)))
				throw new Exception("Can't Search, Please Login first !!!!");
			
		} catch (Exception e) {
			logging.error(e.getMessage());
			throw e;
		}
		
		logging.info("customerCode --> " +  customerCode);
		logging.info("customerName --> " +  customerName);
		logging.info("idNo --> " +  idNo);
		logging.info("taxId --> " +  taxId);
		logging.info("maxResult --> " +  maxResult);
		
		int maxResultInt = -1;
		if(!CommonService.IsNull(maxResult))
			maxResultInt = (Integer.parseInt(CommonService.IsNullRZero(maxResult)) == 0) ? -1 : Integer.parseInt(CommonService.IsNullRZero(maxResult));
		
		List<Customer> lst = iCustomerServices.FindByIs("", "N", "N", maxResultInt);
		logging.info("Customer Size = " + CommonService.getSize(lst));
		
		/*
		for(Customer value : lst)
			logging.info(value.toString());
		 */
		
		if(CommonService.getSize(lst) > 0) 
			return lst;
		
		return null;
	}	
}
